import {
    Component,
    ElementRef,
    EventEmitter,
    Injector,
    Output,
    ViewChild,
} from "@angular/core";
import { AppComponentBase } from "@shared/common/app-component-base";
import {
    PlacesServiceProxy
} from "@shared/service-proxies/service-proxies";
import { ModalDirective } from "ngx-bootstrap";

import { finalize } from "rxjs/operators";

@Component({
    selector: "placeUserModal",
    templateUrl: "./place-user-modal.component.html",
})
export class PlaceUserModalComponent extends AppComponentBase {
    @ViewChild("placeUserModal") modal: ModalDirective;

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    placeId: any;
    placeName: any;
    placeNum: any;
    placeEndTime: any;
    userName: any = '';
    userPhone: any;
    carNums: any;

    constructor(
        injector: Injector,
        private _placesServiceProxy: PlacesServiceProxy
    ) {
        super(injector);
    }

    show(placeId:any): void {
        this.userName = "";
        if(placeId == undefined || placeId == null || placeId == "") {            
            return;
        }
        this._placesServiceProxy.getPlacsUser(placeId).subscribe(result=>{
           
            this.placeId = placeId;
            this.placeName = result.placeName;
            this.placeNum = result.placeNum;
            this.placeEndTime = result.placeEndDate;
            this.userName = result.userName;
            this.userPhone = result.userPhone;
            this.carNums = result.carNums;

            this.modal.show();
        });
    }

    onShown(): void {}

    deleteUserParingInfo(): void {
        this.message.confirm("确定解除绑定",isConfirmed=>{
            if(isConfirmed){
                this._placesServiceProxy.deleteUserParingInfoByPlaceIdAndEndTime(this.placeId)
                .subscribe(result=>{
                    if(result == ""){ 
                        this.modalSave.emit()                       
                        this.modal.hide();

                    }else{
                        this.message.error(result);
                    }
                });
            }
        })
    }

    close(): void {
        this.userName = "";
        this.modal.hide();
    }
}
